import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class OperationWin extends JFrame implements ActionListener {
    JButton showRecord,updateRecord,insertRecord,deleteRecord,exit;
    JLabel hint1,hint2,hint3,hint4;
    InterfaceWin interfaceWin;
    Date date;
    ShowWin showWin;
    UpdateWin updateWin;
    InsertWin insertWin;
    DeleteWin deleteWin;
    String user;
    OperationWin(String title,String user){
        setTitle(title);
        this.user=user;

        hint1=new JLabel("您好！");
        hint2=new JLabel(user);
        date=new Date();
        SimpleDateFormat to = new SimpleDateFormat("yyyy-MM-dd");
        String today=to.format(date);
        hint3=new JLabel(today);
        hint4=new JLabel(getWeekday(date));
        hint1.setFont(new Font("隶书",Font.BOLD,30));
        hint2.setFont(new Font("隶书",Font.BOLD,30));
        hint3.setFont(new Font("隶书",Font.BOLD,30));
        hint4.setFont(new Font("隶书",Font.BOLD,30));
        showRecord=new JButton("显示学生信息");
        updateRecord=new JButton("更新学生信息");
        insertRecord=new JButton("插入学生信息");
        deleteRecord=new JButton("删除学生信息");
        exit=new JButton("退出");

        showRecord.addActionListener(this);
        updateRecord.addActionListener(this);
        insertRecord.addActionListener(this);
        deleteRecord.addActionListener(this);
        exit.addActionListener(this);

        setLayout(null);
        add(hint1);
        add(hint2);
        add(hint3);
        add(hint4);
        hint1.setBounds(90,60,100,50);
        hint2.setBounds(90,80,100,100);
        hint3.setBounds(40,125,300,100);
        hint4.setBounds(75,160,100,100);
        add(showRecord);
        add(updateRecord);
        add(insertRecord);
        add(deleteRecord);
        add(exit);
        showRecord.setBounds(250,50,130,40);
        updateRecord.setBounds(250,100,130,40);
        insertRecord.setBounds(250,150,130,40);
        deleteRecord.setBounds(250,200,130,40);
        exit.setBounds(250,250,130,40);

        setBounds(700,250,450,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public void actionPerformed(ActionEvent event){
        if(event.getSource()==exit){
            dispose();
            interfaceWin=new InterfaceWin();
        }
        else if(event.getSource()==showRecord){
            dispose();
            showWin=new ShowWin("学生信息",user);
        }
        else if(event.getSource()==updateRecord){
            dispose();
            updateWin=new UpdateWin("更新学生信息",user);
        }
        else if(event.getSource()==insertRecord){
            dispose();
            insertWin=new InsertWin("插入学生信息",user);
        }
        else if(event.getSource()==deleteRecord){
            dispose();
            deleteWin=new DeleteWin("删除学生信息",user);
        }
    }

    public String getWeekday(Date date){
        String[] weeks={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int today=calendar.get(Calendar.DAY_OF_WEEK)-1;
        if(today<0)
            today=0;
        return weeks[today];
    }
}
